package com.yylending.las.webview;

import android.text.TextUtils;

import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IEvent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.secure.Cryptos;
import com.yylending.las.Global;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by HenryZhang on 2016/11/2.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class WebViewPresenter extends Presenter<WebViewModel, WebViewPresenter.View> {
	private final static String TAG = "WebViewPresenter";

	public interface View extends IView {
		void responseLoad(int type, String url);

		void responseData(String title, int commentType, int articleId);

		void loadFail(String msg);
	}

	public static class Event extends IEvent {
		public final static int EVENT_RESPONSE_CONTENT = 1;
		public final static int EVENT_RESPONSE_DATA = 2;

		public String content;
		public String title;
		public int commentType;
		public int articleId;
	}

	public static class Generator implements PresenterFactory<WebViewPresenter> {
		@Override
		public WebViewPresenter create() {
			return new WebViewPresenter();
		}
	}

	private View mView;
	private String mRequestPage;

	@Override
	public void attach(View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new WebViewModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	/*@Subscribe(threadMode = ThreadMode.MAIN)
	public void onEventWithAttribute(Event e) {
		switch (e.id) {
			case Event.EVENT_RESPONSE_CONTENT:
				//mView.responseLoad(GlobalData.ArgumentsKey.KEY_WEBVIEW_TYPE_CONTENT, e.content);
				break;
			case Event.EVENT_RESPONSE_DATA:
				mView.responseData(e.title, e.commentType, e.articleId);
				break;
		}
	}*/

	public void loadUrl(String method, String path, String body, final CallBackFunction function) {
		/*WebViewModel.getContent(aty, method, path, body, new OnNextListener<BaseResultEntity<String>>() {
			@Override
			public void onNext(BaseResultEntity<String> rs) {
				CLog.i(TAG, "loadUrl: result=" + rs);
				if(YJKD.isResponseSucc(rs)){
					String content = Cryptos.aesDecrypt(rs.getContent(), Global.KEY_AES);
					CLog.i(TAG, "loadUrl: content=" + content);
					BaseResultEntity<JsonArray> resultEntity = new BaseResultEntity<>();
					resultEntity.setCode(rs.getCode());
					resultEntity.setMsg(rs.getMsg());
					resultEntity.setStatus(rs.getStatus());
					try {
						resultEntity.setContent(new JsonParser().parse("").getAsJsonArray());
					}catch (Exception e) {
						CLog.i(TAG, "loadUrl: e=" + e.toString());
						resultEntity.setContent(new JsonArray());
					}
					CLog.i(TAG, "loadUrl: data=" + resultEntity.toString());
					function.onCallBack(resultEntity.toString());
				}else if(YJKD.isResponseWarning(rs) || YJKD.isResponseError(rs)){
					showToast(rs.getMsg());
				}
			}
		});*/
		execute(new WebViewModel.LoadUrl(method, path, body), FUNCTIONAL_CODE, true, function, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void loadResult(WebViewModel.LoadUrlResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				CLog.i(TAG, "rs=" + result.toString());
				String content = Cryptos.aesDecrypt(result.getData(), Global.KEY_AES);
				CLog.i(TAG, "loadUrl: content=" + content);
				BaseResultEntity<JsonArray> resultEntity = new BaseResultEntity<>();
				resultEntity.setRetCode(result.getRetCode());
				resultEntity.setRetMsg(result.getRetMsg());
				try {
					resultEntity.setData(new JsonParser().parse("").getAsJsonArray());
				} catch (Exception e) {
					CLog.i(TAG, "loadUrl: e=" + e.toString());
					resultEntity.setData(new JsonArray());
				}
				CLog.i(TAG, "loadUrl: data=" + resultEntity.toString());
				CallBackFunction function = (CallBackFunction)result.extraParam;
				function.onCallBack(resultEntity.toString());
			} else {
				String msg = result.getRetMsg();
				mView.loadFail(TextUtils.isEmpty(msg) ? "加载失败" : msg);
			}
		}
	}
}